<template>
  <el-dialog
    v-model="visible"
    :title="form.id ? '修改' : '新增'"
    width="600"
    draggable
    @close="close"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :z-index="2000"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-align="right"
      label-width="120"
      size="default"
      label-position="top"
    >
      <el-row :gutter="20">
        <el-col :span="16">
          <el-form-item
            label="授权代表公司名称"
            prop="importer_company_name"
            label-width="140"
          >
            <el-input v-model="form.importer_company_name" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="form.type">
              <el-radio :label="1" size="large">欧代</el-radio>
              <el-radio :label="2" size="large">英代</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电子邮件" prop="email">
            <el-input v-model="form.email" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="telephone">
            <el-input v-model="form.telephone" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="公司地址">
            <VGroup class="w-full">
              <el-input v-model="form.province" placeholder="省" />
              <el-input v-model="form.city" placeholder="市" />
              <el-input v-model="form.area" placeholder="区" />
            </VGroup>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="详细地址">
            <el-input type="textarea" v-model="form.address" placeholder="详细地址" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="申请公司">
            <el-input v-model="form.applied_company" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="下证日期">
            <el-date-picker
              style="width: 100%"
              v-model="form.issuance_time"
              type="date"
              placeholder="请选择下证日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服务周期">
            <el-date-picker
              v-model="form.service_period"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" />
            <el-checkbox
              v-model="form.is_print_telephone"
              :true-label="1"
              :false-label="0"
              >是否打印电话号码</el-checkbox
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save(formRef)"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>
<script setup name="ComModal">
import { createImporter, editImporter, getImporterDetail } from "../../api";
import { reactive, toRefs } from "vue";
import { ElMessage } from "element-plus";

const emits = defineEmits(["query"]);
const props = defineProps({
  options: {
    type: Object,
    required: true,
  },
});
const data = reactive({
  form: {},
  visible: false,
  loading: false,
  formRef: null,
  rules: {
    importer_company_name: [{ required: true, message: "请输入授权代表公司名称" }],
    type: [{ required: true, message: "请选择类型" }],
    email: [
      { required: true, message: "请输入电子邮箱" },
      {
        type: "email",
        message: "请输入正确电子邮箱",
        trigger: ["blur", "change"],
      },
    ],
    telephone: [{ required: true, message: "请输入联系电话" }],
    address: [{ required: true, message: "请输入详细地址" }],
  },
});
const { rules, form, visible, loading, formRef } = toRefs(data);
const open = (id) => {
  if (id) getDetail(id);
  visible.value = true;
};
const getDetail = (id) => {
  getImporterDetail({ id }).then((res) => {
    form.value = res.data;
    if (form.value.service_period_start && form.value.service_period_end) {
      form.value.service_period = [
        form.value.service_period_start,
        form.value.service_period_end,
      ];
    }
  });
};

const save = () => {
  if (!formRef.value) return;
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      const formData = Object.assign({}, form.value);
      if (formData.service_period?.length) {
        formData.service_period_start = formData.service_period[0];
        formData.service_period_end = formData.service_period[1];
        delete formData.service_period;
      }

      if (form.value.id) {
        editImporter(formData)
          .then((res) => {
            ElMessage.success(res.message);
            emits("query");
            close();
          })
          .finally(() => {
            loading.value = false;
          });
      } else {
        createImporter(formData)
          .then((res) => {
            ElMessage.success(res.message);
            emits("query");
            close();
          })
          .finally(() => {
            loading.value = false;
          });
      }
    }
  });
};
const initForm = () => {
  form.value = getForm();
};
// 初始化
const getForm = () => {
  return {
    importer_company_name: "",
    type: 1,
    email: "",
    telephone: "",
    address: "",
    applied_company: "",
    issuance_time: "",
    service_period: "",
    remark: "",
    is_print_telephone: "",
  };
};

onMounted(() => {
  initForm();
});
const close = () => {
  formRef.value.resetFields();
  initForm();
  visible.value = false;
};
defineExpose({ open });
</script>
